<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        /*构造方式1：如果是无参构造器，则返回当前时间*/
        var date1 = new Date();
        console.log(date1);
        /*构造方式2：如果是有参构造器，如"2022-7-12"，则返回字符串对应的时间*/
        var date2 = new Date("2022-7-12");
        console.log(date2);

        /*date对象常用方法*/
        console.log(date1.getFullYear());//获取当年
        console.log(date1.getMonth());//获取当月
        console.log(date1.getDate());//获取当天
        console.log(date1.getDay());//获取星期几
        console.log(date1.getHours());//获取当前时
        console.log(date1.getMinutes());//获取当前分
        console.log(date1.getSeconds());//获取当前秒

        /*日期格式化*/
        var date3 = new Date();
        var year = date3.getFullYear();
        var month = date3.getMonth();
        var day = date3.getDate();
        var dayOfWeek = date3.getDay();
        var arr = ['星期日','星期一1','星期二','星期三','星期四','星期五','星期六'];
        function gettime() {
            var h = date1.getHours();
            var m = date1.getMinutes();
            var s = date1.getSeconds();
            return h+':'+m+':'+s;
        }
        console.log('今天是：'+year+'年'+month+'月'+day+'日'+arr[dayOfWeek]+gettime());
    </script>
</body>
</html>